package com.denlaku.longan.controller.impl;

import com.denlaku.longan.anno.VarBy;
import com.denlaku.longan.controller.DataTagRelController;
import com.denlaku.longan.http.Resp;
import com.denlaku.longan.http.Return;
import com.denlaku.longan.qo.DataPermQuery;
import com.denlaku.longan.service.DataTagRelService;
import com.denlaku.longan.util.SnowflakeUtils;
import com.denlaku.longan.vo.DataTagRel;
import com.denlaku.longan.vo.PageBy;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author tianx
 */
@RestController
public class DataTagRelControllerImpl implements DataTagRelController {
    @Autowired
    private DataTagRelService dataTagRelService;

    @Override
    public Return<Void> batchAdd(List<DataTagRel> dataTagRelList) {
        ListUtils.emptyIfNull(dataTagRelList).forEach(rel -> rel.setId(SnowflakeUtils.nextId()));
        dataTagRelService.batchAdd(dataTagRelList);
        return Resp.success();
    }

    @VarBy
    @Override
    public Return<List<DataTagRel>> pageList(DataPermQuery query, PageBy pageBy) {
        PageHelper.startPage(pageBy.getPage(), pageBy.getSize());
        List<DataTagRel> list = dataTagRelService.list(query);
        return Resp.success(list);
    }

    @Override
    public Return<Void> delete(DataPermQuery query) {
        if (query.getId() != null) {
            dataTagRelService.delete(query);
        }
        return Resp.success();
    }
}
